<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="showAddCate"
          >添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 分类列表展示 -->
      <zk-table
        show-index
        index-text="#"
        border
        :show-row-hover="false"
        :selection-type="false"
        :data="cateList"
        :columns="columns"
        :expand-type="false"
      >
        <template
          slot="isok"
          slot-scope="scope"
        >
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: yellowgreen;"
          ></i>
          <i
            class="el-icon-error"
            v-else
            style="color: deeppink;"
          ></i>
        </template>
        <template
          slot="order"
          slot-scope="scope"
        >
          <el-tag
            size="mini"
            type="primary"
            v-if="scope.row.cat_level === 0"
          >一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 1"
          >二级</el-tag>
          <el-tag
            size="mini"
            type="warning"
            v-else
          >三级</el-tag>
        </template>
        <template
          slot="opt"
        >
          <el-button
            size="mini"
            icon="el-icon-edit"
            type="primary"
          >编辑</el-button>
          <el-button
            size="mini"
            icon="el-icon-delete"
            type="danger"
          >删除</el-button>
        </template>
      </zk-table>
      <!-- 分页功能展示 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querData.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="querData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类的面板 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialog"
      width="50%"
      @close="closeAddCate"
    >

      <!-- 添加分类表单的具体内容 -->
      <el-form
        :model="cateForm"
        :rules="cateFormRule"
        ref="cateFormRef"
        label-width="100px"
      >
        <el-form-item
          label="活动名称："
          prop="cat_name"
        >
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            v-model="selectKeys"
            :options="parentCateList"
            :props="cascaderProps"
            clearable
            change-on-select
            @change="changeCate"
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addCateDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCateData"
        >确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      querData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品总条数
      total: 0,
      columns: [
        {
          prop: 'cat_name',
          label: '商品分类名称'
        },
        {
          prop: 'cat_deleted',
          label: '是否有效',
          // 表示自定义模板列
          type: 'template',
          // 模板、插槽名称
          template: 'isok'
        },
        {
          label: '等级标签',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateDialog: false,
      // 数据总控
      cateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 规则总控
      cateFormRule: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 存储的是父级分类的数据
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectKeys: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      let { data: res } = await this.$http.get('categories', {
        params: this.querData
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品分类列表数据请求失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.querData.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(curPage) {
      this.querData.pagenum = curPage
      this.getCateList()
    },
    showAddCate() {
      // 调用获取父级分类数据的方法
      this.parentCateData()
      this.addCateDialog = true
    },
    // 获取父级分类数据
    async parentCateData() {
      let { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error('数据请求失败')
      // 请求的数据放置在data中，供页面使用
      this.parentCateList = res.data
    },
    changeCate() {
      // 每次用户选择了分类，都会触发这个事件
      // 如果 this.selectKeys 有内容，则是有分类，默认则是一级分类
      if (this.selectKeys.length > 0) {
        this.cateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.cateForm.cat_level = this.selectKeys.length
      } else {
        // 全部重置为 0
        this.cateForm.cat_pid = this.cateForm.cat_level = 0
      }
    },
    addCateData() {
      // console.log(this.cateForm)
      this.$refs.cateFormRef.validate(async valid => {
        if (!valid) return
        let { data: res } = await this.$http.post('categories', this.cateForm)
        // 判断解构出来的返回数据是否成功
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        // 添加分类成功
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialog = false
      })
    },
    closeAddCate() {
      this.$refs.cateFormRef.resetFields()
      this.selectKeys = []
      this.cateForm.cat_pid = this.cateForm.cat_level = 0
    }
  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 20px;
  i {
    font-size: 20px;
  }
}

.el-pagination {
  margin-top: 20px;
}

.el-cascader {
  width: 100%;
}
</style>